package com.byxy.luntan.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.byxy.luntan.entity.User;

import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LoginInterceptor implements HandlerInterceptor{
	
	/*
	 * 进入Controller这前
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		log.info("+++++++++++preHandle");
		log.info(handler.toString());
		User u = (User) request.getSession().getAttribute("USER");
		if (u == null) {
			response.sendRedirect("login.jsp");
			return false;
		}
		return HandlerInterceptor.super.preHandle(request, response, handler);
	}

	/*
	 * 退出Controller这前 return 之前
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		log.info("+++++++++++postHandle");
		HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
	}

	/*
	 * Controller结束后 return 这后
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		log.info("+++++++++++afterCompletion");
		HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
	}
	
	
}
